package com.zjcloud.academic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjcloud.common.annotation.Log;
import com.zjcloud.common.core.controller.BaseController;
import com.zjcloud.common.core.domain.AjaxResult;
import com.zjcloud.common.enums.BusinessType;
import com.zjcloud.academic.domain.AcCourseSchedule;
import com.zjcloud.academic.service.IAcCourseScheduleService;
import com.zjcloud.common.utils.poi.ExcelUtil;
import com.zjcloud.common.core.page.TableDataInfo;

/**
 * 课表管理Controller
 * 
 * @author zjcloud
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/academic/courseSchedule")
public class AcCourseScheduleController extends BaseController
{
    @Autowired
    private IAcCourseScheduleService acCourseScheduleService;

    /**
     * 查询课表管理列表
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcCourseSchedule acCourseSchedule)
    {
        startPage();
        List<AcCourseSchedule> list = acCourseScheduleService.selectAcCourseScheduleList(acCourseSchedule);
        return getDataTable(list);
    }

    /**
     * 根据学期查询课表列表
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:list')")
    @GetMapping("/semester/{semesterId}")
    public AjaxResult getScheduleBySemester(@PathVariable Long semesterId)
    {
        List<AcCourseSchedule> list = acCourseScheduleService.selectAcCourseScheduleBySemesterId(semesterId);
        return AjaxResult.success(list);
    }

    /**
     * 根据日期范围查询课表列表
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:list')")
    @GetMapping("/date-range")
    public AjaxResult getScheduleByDateRange(String startDate, String endDate, Long semesterId)
    {
        List<AcCourseSchedule> list = acCourseScheduleService.selectAcCourseScheduleByDateRange(startDate, endDate, semesterId);
        return AjaxResult.success(list);
    }

    /**
     * 导出课表管理列表
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:export')")
    @Log(title = "课表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody AcCourseSchedule acCourseSchedule)
    {
        List<AcCourseSchedule> list = acCourseScheduleService.selectAcCourseScheduleList(acCourseSchedule);
        ExcelUtil<AcCourseSchedule> util = new ExcelUtil<AcCourseSchedule>(AcCourseSchedule.class);
        util.exportExcel(response, list, "课表管理数据");
    }

    /**
     * 获取课表管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acCourseScheduleService.selectAcCourseScheduleById(id));
    }

    /**
     * 新增课表管理
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:add')")
    @Log(title = "课表管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcCourseSchedule acCourseSchedule)
    {
        return toAjax(acCourseScheduleService.insertAcCourseSchedule(acCourseSchedule));
    }

    /**
     * 修改课表管理
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:edit')")
    @Log(title = "课表管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcCourseSchedule acCourseSchedule)
    {
        return toAjax(acCourseScheduleService.updateAcCourseSchedule(acCourseSchedule));
    }

    /**
     * 删除课表管理
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:remove')")
    @Log(title = "课表管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acCourseScheduleService.deleteAcCourseScheduleByIds(ids));
    }

    /**
     * 根据学期删除课表
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:remove')")
    @Log(title = "课表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/semester/{semesterId}")
    public AjaxResult removeBySemester(@PathVariable Long semesterId)
    {
        return toAjax(acCourseScheduleService.deleteAcCourseScheduleBySemesterId(semesterId));
    }

    /**
     * 生成排课任务
     */
    @PreAuthorize("@ss.hasPermi('academic:courseSchedule:generate')")
    @Log(title = "课表管理", businessType = BusinessType.GENCODE)
    @PostMapping("/generate/{semesterId}/{algorithmConfigId}")
    public AjaxResult generateSchedule(@PathVariable Long semesterId, @PathVariable Long algorithmConfigId)
    {
        boolean result = acCourseScheduleService.generateCourseSchedule(semesterId, algorithmConfigId);
        if (result) {
            return AjaxResult.success("排课任务生成成功");
        } else {
            return AjaxResult.error("排课任务生成失败");
        }
    }
}